package com.sk.bilibili.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sk.bilibili.bean.Focus;
import com.sk.bilibili.bean.Member;
import com.sk.bilibili.event.BiliBiliEvent;
import com.sk.bilibili.other.R;
import com.sk.bilibili.utils.DbUtils;
import com.sk.bilibili.utils.BSessionUtils;
import com.sk.bilibili.utils.QueryWrapperUtils;
import com.sk.bilibili.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import com.sk.bilibili.bean.FocusGroup;
import com.sk.bilibili.mapper.FocusGroupMapper;

import java.util.List;

/**
 * @author 邵康
 * @date 2021/11/26 21:00
 */
@Service
public class FocusGroupService extends ServiceImpl<FocusGroupMapper, FocusGroup> {

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private FocusService focusService;
    @Autowired
    private ApplicationEventPublisher eventPublisher;
    private static final String DEFAULT_FOCUS_GROUP_NAME = "默认分组";

    public R<Void> add(FocusGroup focusGroup) {
        focusGroup.setMemberId(BSessionUtils.getMemberId());
        save(focusGroup);
        return R.success();
    }

    public R<Void> delete(Integer id) {
        FocusGroup focusGroup = getById(id);
        if (focusGroup.getRoot()) return R.error("不能删除默认分组");
        removeById(id);
        eventPublisher.publishEvent(new BiliBiliEvent<>(this, id));
        return R.success();
    }

    public R<Void> update(FocusGroup focusGroup) {
        updateById(focusGroup);
        return R.success();
    }

    public R<IPage<FocusGroup>> page(Integer page, Integer size) {
        return R.success(page(new Page<>(page, size), null));
    }

    public R<List<FocusGroup>> getAll() {
        QueryWrapper<FocusGroup> queryWrapper = QueryWrapperUtils.eq("member_id", BSessionUtils.getMemberId());
        return R.success(list(queryWrapper));
    }

    public Integer getDefaultFocusGroup(int memberId) {
        return baseMapper.getDefaultFocusGroup(memberId);
    }

    @EventListener(condition = "event.type == 'com.sk.bilibili.service.FocusService.update'")
    public void handleFocusUpdate(BiliBiliEvent<Focus> event) {
        Focus focus = event.getData();
        Focus temp = focusService.getById(focus.getId());
        changeCount(focus.getFocusGroupId(),1);
        changeCount(temp.getFocusGroupId(),-1);
    }

    private void changeCount(Integer focusGroupId, int num) {
        redisUtils.changeNum("FocusGroup-"+focusGroupId,"count",num);
    }

    @EventListener(condition = "event.type == 'com.sk.bilibili.service.FocusService.switchFocus'")
    public void handleFocusSwitch(BiliBiliEvent<Focus> event) {
        Focus focus = event.getData();
        Integer focusGroupId = event.getExtra("focusGroupId");
        changeCount(focusGroupId,focus == null ? -1 : 1);
    }

    @EventListener(condition = "event.type == 'com.sk.bilibili.service.MemberService.register'")
    public void handleMemberRegister(BiliBiliEvent<Member> event) {
        Member member = event.getData();
        FocusGroup focusGroup = new FocusGroup();
        focusGroup.setMemberId(member.getId());
        focusGroup.setRoot(true);
        focusGroup.setTitle(DEFAULT_FOCUS_GROUP_NAME);
        save(focusGroup);
    }

}
